<?php
/**
 * Created by PhpStorm.
 * User: xunuo
 * Date: 2017/6/27
 * Time: 13:22
 */

namespace app\admin\model;
use think\Model;

/**
 * Class Outtime
 * @package app\admin\model
 * 过期模型
 */
class Outtime extends Model
{
    /**
     * @param $pageNum
     * @param $numPerPage
     * @param $order_sn
     * @param $company_name
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 订单列表
     */
    public function getList($pageNum, $numPerPage, $order_sn, $company_name)
    {
        $map = [];
        if (!empty($order_sn)) {
            $map['order_sn'] = ['like', "%{$order_sn}%"];
        }
        if (!empty($company_name)) {
            $company_id = [];
            //根据商家名称搜索出商家id数组
            $cmopany_id_array = db('company')
                    ->field('id')
                    ->where('company_name', 'like', "%{$company_name}%")
                    ->select();
            foreach ($cmopany_id_array as $val_company_id) {
                $company_id[] = $val_company_id['id'];
            }
            $map['company_id'] = array('in', $company_id);
        }
        $map['order_status'] = 2;
        $order = db("order")
                ->field("open_id")
                ->where($map)
                ->select();
        foreach ($order as $k=>$v){
            $open[$k] = $v['open_id'];
        }
        $dl['open_id'] = array('in',$open);
        $dls = db("driving_license")
                ->field("license_register_date")
                ->where($dl)
                ->select();
        $map['open_id'] = array('in',$open);
        foreach ($dls as $kk=>$vv){
            $dates = strtotime($vv['license_register_date']."00:00:00");
            $order_detail = db("order")
                ->where($map)
                ->where('subscribe_date','gt',$dates)
                ->page($pageNum, $numPerPage)
                ->order('company_id,create_time desc')
                ->select();
        }
        foreach ($order_detail as $key => $value) {
            $company = db('company')->where('id',$value['company_id'])->find();
            $order_detail[$key]['company_name'] = $company['company_name'];
            $user = db('users')->where('open_id',$value['open_id'])->find();
            $order_detail[$key]['username'] = $user['username'];
            $order_detail[$key]['address'] = $user['address'];
            $order_detail[$key]['usermobile'] = $user['mobile'];
        }
        $data['list'] = $order_detail;
        return $data;
    }
    /**
     * @param $order_sn
     * @param $company_name
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 订单总数
     */
    public function getCount($order_sn, $company_name)
    {
        $map = [];
        if (!empty($order_sn)) {
            $map['order_sn'] = ['like', "%{$order_sn}%"];
        }
        if ($company_name != '') {
            $company_id = [];
            //根据商家名称搜索出商家id数组
            $cmopany_id_array = db('company')
                    ->field('id')
                    ->where('company_name', 'like', "%{$company_name}%")
                    ->select();
            foreach ($cmopany_id_array as $val_company_id) {
                $company_id[] = $val_company_id['id'];
            }
            $map['company_id'] = array('in', $company_id);
        }
        $map['order_status'] = 2;
        $order = db("order")
            ->field("open_id")
            ->where($map)
            ->select();
        foreach ($order as $k=>$v){
            $open[$k] = $v['open_id'];
        }
        $dl['open_id'] = array('in',$open);
        $dls = db("driving_license")
            ->field("license_register_date")
            ->where($dl)
            ->select();
        $map['open_id'] = array('in',$open);
        foreach ($dls as $kk=>$vv){
            $dates = strtotime($vv['license_register_date']."00:00:00");
            $count = db("order")
                ->field("order_id")
                ->where($map)
                ->where('subscribe_date','gt',$dates)
                ->count();
        }
        return $count;
    }
    /**
     * @param $reback_id
     * @param $open_id
     * @throws \Exception
     * 平台确认退款
     */
    public function dosurereback($reback_id,$open_id) {
        //改订单状态
        db('order')->where('order_id', $reback_id)->setField('refund_status', 3);
        db('order')->where('order_id', $reback_id)->setField('update_time', time());
        //生成消息记录
        $mapa = array(
            'title' => '退款通知',
            'message' => '订单编号为:' . get_order_sn($reback_id) . ' ，已为您处理退款',
            'open_id' => $open_id,
            'status' => 0,
            'is_delete' => 0,
            'send_time' => time()
        );
        $result_message = db('message')->insert($mapa);
        if (!$result_message) {
            exception('添加失败message');
        }
    }
}